package com.digitalchina.lxnsy.update;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.xml.UpdateCancelXML;

import org.lzh.framework.updatepluginlib.creator.DialogCreator;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.SafeDialogOper;

public class CustomNeedUpdateCreator extends DialogCreator {


    @Override
    public Dialog create(final Update update, final Activity activity) {

//        if (activity == null || activity.isFinishing()) {
//            Log.e("DialogCreator--->", "Activity was recycled or finished,dialog shown failed!");
//            return null;
//        }

        final Dialog dialog = new Dialog(activity, R.style.NoBackGroundDialog);

        String updateContent = activity.getText(org.lzh.framework.updatepluginlib.R.string.update_version_name)
                + ": " + update.getVersionName() + "\n\n\n"
                + update.getUpdateContent();

        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_custom_update, null);
        TextView tvTitle = (TextView) view.findViewById(R.id.view_update_tv_title);
        TextView tvContent = (TextView) view.findViewById(R.id.view_update_tv_content);
        Button btUpdate = (Button) view.findViewById(R.id.view_update_btn_update);
        ImageView ivCancel = (ImageView) view.findViewById(R.id.view_update_iv_cancel);

        tvTitle.setText(R.string.update_title);
        tvContent.setText(updateContent);
        btUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!update.isForced()) {
                    SafeDialogOper.safeDismissDialog(dialog);
                }
                sendDownloadRequest(update);
            }
        });
        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpdateCancelXML.setCancelTime(activity, String.valueOf(System.currentTimeMillis())); // 存储取消更新时间，用于24小时候再次提醒，避免频繁提醒
                sendUserCancel();
                SafeDialogOper.safeDismissDialog(dialog);
            }
        });


        dialog.setCancelable(false);

        // 如果为强制更新 则不显示取消按钮
        if (update.isForced())
            ivCancel.setVisibility(View.GONE);
        else
            ivCancel.setVisibility(View.VISIBLE);
        dialog.setContentView(view);

//        MaterialDialog.Builder builder = new MaterialDialog.Builder(activity)
//                .title(R.string.update_title)
//                .content(updateContent)
//                .positiveText(R.string.update_immediate)
//                .onPositive(new MaterialDialog.SingleButtonCallback() {
//                    @Override
//                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                        if (!update.isForced()) {
//                            SafeDialogOper.safeDismissDialog(dialog);
//                        }
//                        sendDownloadRequest(update);
//                    }
//                });
//        if (update.isIgnore() && !update.isForced()) {
//            builder.negativeText(R.string.update_ignore)
//                    .onNegative(new MaterialDialog.SingleButtonCallback() {
//                        @Override
//                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            sendUserIgnore(update);
//                            SafeDialogOper.safeDismissDialog(dialog);
//                        }
//                    });
//        }
//
//        if (!update.isForced()) {
//            builder.negativeText(R.string.update_cancel)
//                    .onNegative(new MaterialDialog.SingleButtonCallback() {
//                        @Override
//                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            UpdateCancelXML.setCancelTime(activity, String.valueOf(System.currentTimeMillis())); // 存储取消更新时间，用于24小时候再次提醒，避免频繁提醒
//                            sendUserCancel();
//                            SafeDialogOper.safeDismissDialog(dialog);
//                        }
//                    });
//        }
//        builder.cancelable(false);
        return dialog;
    }
}
